package com.ruoyi.ebike.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.ebike.domain.Vehicle;
import com.ruoyi.ebike.vo.DeptVO;
import com.ruoyi.ebike.service.IDeployService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/ebike/deploy")
public class DeployController extends BaseController {
    @Autowired
    private IDeployService deployService;

    /**
     * 单个导入车辆
     */
    @PreAuthorize("@ss.hasPermi('ebike:deploy:add')")
    @Log(title = "车辆管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult addSingleVehicle(@RequestBody Vehicle vehicle) {
        try {
            return toAjax(deployService.addSingleVehicle(vehicle));
        } catch (DataIntegrityViolationException e) {
            return AjaxResult.error("车辆编号已存在，请使用唯一的车辆编号。");
        } catch (Exception e) {
            return AjaxResult.error("添加车辆时发生错误，请稍后重试。");
        }
    }

    // 获取当前用户下级部门列表
    @GetMapping("/list")
    public AjaxResult fetchDeptlist() {
        List<SysDept> deptlist = deployService.fetchDeptlist();
        AjaxResult ajax = AjaxResult.success();
        ajax.put("deptlist", deptlist);
        return ajax;
    }

    /**
     * 批量导入车辆
     */
    @PreAuthorize("@ss.hasPermi('ebike:deploy:addBatch')")
    @Log(title = "车辆管理", businessType = BusinessType.INSERT)
    @PostMapping("/addBatch")
    public AjaxResult addBatchVehicles(@RequestBody List<Vehicle> vehicles) {
        try {
            int result = deployService.addBatchVehicles(vehicles);
            return toAjax(result);
        }catch (DataIntegrityViolationException e) {
            return AjaxResult.error("车辆编号已存在，请使用唯一的车辆编号。");
        } catch (Exception e) {
            return AjaxResult.error("批量导入发生错误，请稍后重试。");
        }
    }

    /**
     * 批量更新车辆
     */
    @PreAuthorize("@ss.hasPermi('ebike:deploy:updateBatch')")
    @Log(title = "车辆管理", businessType = BusinessType.UPDATE)
    @PostMapping("/updateBatch")
    public AjaxResult updateBatchVehicles(@RequestBody List<Vehicle> vehicles) {
        try {
            int result = deployService.updateBatchVehicles(vehicles);
            if (result == 0) {
                return AjaxResult.error("批量修改失败，车辆不存在。");
            }
            return toAjax(result);
        } catch (Exception e) {
            return AjaxResult.error("批量修改时发生错误，请稍后重试。");
        }
    }

    @Log(title = "车辆管理", businessType = BusinessType.UPDATE)
    @PostMapping("/updateDeployStatus")
    public AjaxResult updateDeployStatus(@RequestBody List<Vehicle> vehicles) {
        try {
            int result = deployService.updateDeployStatus(vehicles);
            if (result == 0) {
                return AjaxResult.error("批量投放失败，车辆不存在。");
            }
            return toAjax(result);
        } catch (Exception e) {
            return AjaxResult.error("批量投放时发生错误，请稍后重试。");
        }

    }


    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, String type) throws Exception {
        ExcelUtil<Vehicle> util;

        switch (type) {
            case "import":
            case "update":
            case "deploy":
                util = new ExcelUtil<>(Vehicle.class);
                break;
            default:
                return AjaxResult.error("Invalid import type");
        }

        List<Vehicle> vehicleList = util.importExcel(file.getInputStream());

        return AjaxResult.success(vehicleList);
    }



}
